<template>
  <div class="login-container">
    <div style="position: absolute; top: 10%; text-align: center; color: white; font-size: 50px; font-weight: bold">沈阳航空航天大学计算机学院推免系统</div>
    <div class="login-box">
      <div style="font-weight: bold; font-size: 24px; text-align: center; margin-bottom: 30px; color: white">欢 迎 登 录</div>
      <el-form ref="formRef" :model="data.form" :rules="data.rules">
        <el-form-item prop="username">
          <el-input :prefix-icon="User" size="large" v-model="data.form.username" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input show-password :prefix-icon="Lock" size="large" v-model="data.form.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item prop="role">
          <el-select size="large" v-model="data.form.role">
            <el-option value="STUDENT" label="学生"></el-option>
            <el-option value="COLLEGE_ADMIN" label="学院管理员"></el-option>
            <el-option value="SCIENCE_ADMIN" label="研究生院管理员"></el-option>
            <el-option value="SUPER_ADMIN" label="超级管理员"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="large" type="primary" style="width: 100%; background-color: #227eff; border-color: #227eff" @click="login">登 录</el-button>
        </el-form-item>
        <div style="text-align: right; color: white">
          还没有账号？请 <a style="color: orange" href="/register">注册</a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";

localStorage.clear();
const data = reactive({
  form: { role: 'STUDENT' },
  rules: {
    username: [
      { required: true, message: '请输入账号', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' }
    ]
  }
})

const formRef = ref()

// const login = () => {
//   formRef.value.validate(valid => {
//     if (valid) { // 表示表单校验通过
//       request.post('/login', data.form).then(res => {
//         if (res.code === '200') {
//           ElMessage.success('登录成功')
//           // 存储用户信息到浏览器的缓存
//           localStorage.setItem('xm-user', JSON.stringify(res.data))
//           router.push('/manager/home')
//         } else {
//           console.log(res.msg)
//           ElMessage.error(res.msg)
//         }
//       })
//     }
//   })
// }
const login = () => {
  formRef.value.validate(valid => {
    if (valid) { // 表单校验通过
      request.post('/login', data.form).then(res => {
        console.log(data);
        if (res.code === '200') {
          ElMessage.success('登录成功');
          // 存储用户信息到浏览器的缓存
          console.log(res);
          localStorage.setItem('xm-user', JSON.stringify(res.data));
          router.push('/manager/home');
        } else {
          // 后端返回了错误码和消息
          ElMessage.error(res.msg);  // 显示密码错误等信息
        }
      }).catch(error => {
        // 捕获网络请求或其他错误
        console.error(error);
        ElMessage.error('请求失败');
      });
    }
  });
};

</script>

<style scoped>
.login-container {
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("@/assets/imgs/bg.jpg");
  background-size: cover;
}
.login-box {
  width: 350px;
  padding: 30px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  background-color: rgba(0, 0, 0, 0.3);
}
</style>